from datetime import datetime
import random
from tortoise.timezone import make_aware

from core.base_db import BaseModel
from tortoise import fields, BaseDBAsyncClient


async def generate_order_sn():
    ntc_now = make_aware(datetime.now())
    temp_sn = '{}{}{}'.format(ZhanOrder.ORDER_SN_PREFIX, ntc_now.strftime('%Y%m%d%H%M%S%f'), random.randint(100000, 999999))
    order_count = await ZhanOrder.filter(order_sn=temp_sn).count()
    if order_count > 0:
        order_sn = generate_order_sn()
    else:
        order_sn = temp_sn
    return order_sn


class ZhanOrder(BaseModel):
    ORDER_SN_PREFIX = ''

    PAYMENT_TYPE_HUPIJIAO = 1

    PAYMENT_STATUS_UNPAID = 1
    PAYMENT_STATUS_PAID = 2
    PAYMENT_STATUS_PARTIALLY_REFUNDED = 3

    CURRENCY_USD = 1
    CURRENCY_CNY = 2

    order_sn = fields.CharField(max_length=64, db_index=True, unique=True, null=False, default=generate_order_sn)
    goods = fields.ForeignKeyField(model_name="models.ZhanGoods", on_delete=fields.SET_NULL, null=True)
    completed_at = fields.DatetimeField(null=True)  # 订单完成时间
    status = fields.CharField(max_length=255, null=True)  # 订单状态
    shipping_address = fields.JSONField(null=True)  # 收货地址
    billing_address = fields.JSONField(null=True)  # 账单地址
    payment_method = fields.SmallIntField(null=False, default=PAYMENT_TYPE_HUPIJIAO)  # 支付方式
    payment_status = fields.SmallIntField(null=True, default=PAYMENT_STATUS_UNPAID)  # 支付状态
    fulfillment_status = fields.CharField(max_length=255, null=True)  # 发货状态
    currency = fields.SmallIntField(null=True, default=CURRENCY_CNY)  # 货币
    total_price = fields.DecimalField(max_digits=10, decimal_places=2, null=True)  # 总价
    subtotal_price = fields.DecimalField(max_digits=10, decimal_places=2, null=True)  # 小计
    total_tax = fields.DecimalField(max_digits=10, decimal_places=2, null=True)  # 税费
    shipping_price = fields.DecimalField(max_digits=10, decimal_places=2, null=True)  # 运费
    discount_price = fields.DecimalField(max_digits=10, decimal_places=2, null=True)  # 折扣
    customer = fields.ForeignKeyField(model_name="models.ZhanUser")  # 客户
    customer_email = fields.CharField(max_length=255, null=True)  # 客户邮箱
    customer_phone = fields.CharField(max_length=255, null=True)  # 客户电话
    line_items = fields.JSONField(null=True)  # 订单项

    class Meta:
        table = "zhan_order"


class ZhanGoods(BaseModel):
    STATUS_OPEN = 1
    STATUS_CLOSE = 2

    CATEGORY_CHATGPT = 1

    name = fields.CharField(max_length=255, null=False, description="商品名称")
    price = fields.DecimalField(max_digits=10, decimal_places=2, null=False, description="商品价格")
    status = fields.IntField(default=STATUS_OPEN, description="商品状态")
    image = fields.CharField(max_length=255, description="商品图片", null=True)
    description = fields.TextField(description="商品描述", null=True)
    activity = fields.ForeignKeyField(model_name="models.ZhanDiscountActivity", null=True, description="活动")
    category = fields.IntField(description="商品分类", null=True, default=CATEGORY_CHATGPT)
    stock = fields.IntField(default=0, description="库存")

    class Meta:
        table = "zhan_goods"


class ZhanDiscountActivity(BaseModel):
    STATUS_OPEN = 1
    STATUS_CLOSE = 2

    ACTIVITY_AMOUNT = 1
    ACTIVITY_DISCOUNT = 2

    name = fields.CharField(max_length=255, null=False, description="活动名称")
    discount_price = fields.DecimalField(max_digits=10, decimal_places=2, null=False, description="活动折扣价格")
    status = fields.IntField(default=STATUS_OPEN, description="活动状态")
    image = fields.CharField(max_length=255, description="活动图片", null=True)
    description = fields.TextField(description="活动描述", null=True)
    activity_type = fields.SmallIntField(description="活动类型", null=True, default=ACTIVITY_DISCOUNT)
    stock = fields.IntField(default=0, description="库存")
    start_time = fields.DatetimeField(null=True, description="活动开始时间")
    end_time = fields.DatetimeField(null=True, description="活动结束时间")

    class Meta:
        table = "zhan_discount_activity"